package com.bsj.travel.util;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * 数据处理工具类
 */
public class DataUtil {
    /**
     * 横线
     */
    private final static String striping = "-";

    /**
     * 分页
     *
     * @param list
     * @param pageSize
     * @param pageNumber
     * @param <T>
     * @return
     */
    public static <T> List<T> pagingByList(List<T> list, int pageSize, int pageNumber) {
        if (list == null || list.isEmpty() || pageSize <= 0 || pageNumber <= 0) {
            return list;
        } else {
            int size = list.size();
            // 开始索引
            int startIndex = (pageNumber - 1) * pageSize;
            // 结束索引
            int endIndex = startIndex + pageSize;
            if (startIndex > size) {
                return list;
            } else {
                if (endIndex > size) {
                    endIndex = size;
                }
                return list.subList(startIndex, endIndex);
            }
        }
    }

    /**
     * Object 强制转化为List
     *
     * @param obj
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> List<T> castList(Object obj, Class<T> clazz) {
        List<T> result = new ArrayList<T>();
        if (obj instanceof List<?>) {
            for (Object o : (List<?>) obj) {
                result.add(clazz.cast(o));
            }
            return result;
        }
        return null;
    }

    /**
     * 是否在线
     *
     * @param time
     * @return
     */
    public static boolean isOnLine(long time) {
        long systemTime = System.currentTimeMillis();

        // 系统时间减终端时间的差值，单位毫秒
        long diff = systemTime - time;
        if (diff <= 15 * 60 * 1000) {
            return true;
        } else {
            return false;
        }
    }


    /**
     * 解析设备名称并组合成需要的名称
     *
     * @param namePrefix    名称前缀
     * @param deviceSegment 名称段
     * @return List<String> 所有设备名称的集合
     */
    public static List<String> parserDeviceName(String namePrefix, String deviceSegment) {
        List<String> deviceList = new LinkedList<>();
        //1.解析后缀卡段
        String[] arrayStr = deviceSegment.split(striping);
        if (arrayStr == null || arrayStr.length == 0) {
            return null;
        } else if (arrayStr.length == 1) {
            deviceList.add(namePrefix + arrayStr[0]);
            return deviceList;
        }
        //号段形式
        if (arrayStr.length != 2 || arrayStr[0].length() != arrayStr[1].length()) {
            return null;
        }
        int len = arrayStr[0].length();//后缀字符长度
        int allLen = namePrefix.length() + len;//卡号总长度
        //设备名长度必须大于3
        if (allLen <= 3) {
            return null;
        }
        String sDate = arrayStr[0];
        String eDate = arrayStr[1];
        long start = Long.parseLong(sDate);
        long end = Long.parseLong(eDate);
        long j = end - start;
        if (start >= end) {
            return null;
        }
        for (int i = 0; i <= j; i++) {
            deviceList.add((namePrefix + getZero(start, len) + "").trim());
            start = start + 1;
        }
        return deviceList;
    }

    /**
     * 补零
     *
     * @param a   需要补0的数字
     * @param num 补零的位数
     * @return
     */
    public static String getZero(long a, int num) {
        String str = a + "";
        int length = str.length();
        if (length < num) {
            int i = num - length;
            for (int j = 0; j < i; j++) {
                str = "0" + str;
            }
        }
        return str;
    }

    /**
     * 数据分组，用于数据量大的情况。
     *
     * @param list 数据集合
     * @param num  一组多少个数据
     * @return
     */
    public static <T> List<List<T>> splitData(List<T> list, int num) {
        List<List<T>> lists = new ArrayList<List<T>>();
        if (list == null || list.isEmpty()) {
            return lists;
        }

        // 一组数据个数小于等于0，那么默认100个一组。
        if (num <= 0) {
            num = 100;
        }

        int size = list.size(); // 总数
        int count = size / num; // 分组数
        if (size <= num) {
            lists.add(list);
        } else {
            List<T> subList = new LinkedList<T>();
            for (int i = 0; i <= count; i++) {
                if (i == count) {
                    subList = list.subList(i * num, size);
                } else {
                    subList = list.subList(i * num, num * (i + 1));
                }
                if (!subList.isEmpty()) {
                    lists.add(subList);
                }
            }
        }

        return lists;
    }

    /**
     * 防止sql注入
     *
     * @param name
     * @return java.lang.String
     * @time 2022/9/20 10:54
     * @author ljx
     */
    public static String escape(String name) {
        if (null != name && (name.contains("_") || name.contains("%") || name.contains("#"))) {
            char[] chars = name.toCharArray();
            StringBuilder result = new StringBuilder();
            for (char ch : chars) {
                if (ch == '_' || ch == '%' || ch == '#') {
                    result.append("\\");
                }
                result.append(ch);
            }
            return result.toString();
        }
        return name;
    }

    public static void main(String[] args) {
//        String namePrefix = "aaa87555_";
//         boolean b = namePrefix.matches(SalaryConsts.DEVICE_NAME_REGULAT);
//        JsonResultEnum aaaaa = judgeSegment("aaaaa", "0002-1000", "0001-1000");
//        System.out.println(aaaaa);
    }
}
